<template>
  <div class="myCoursesPageNav">
    <div class="container">
      <!-- <router-link
        :to="{ name: item.routeName }"
        v-for="(item, index) in filterRoute"
        :key="index"
      > -->
      <div
        class="flexBox"
        v-for="(item, index) in filterRoute"
        :key="index"
        @click="changePage(item.routeName)"
      >
        <div>
          <img :src="require('@/assets/images/carouselRight.png')" alt="" />
        </div>
        <div class="title" :class="{ isActive: currPage == item.routeName }">
          {{ item.name }} ({{getCourseCount(item.routeName)}})
        </div>
      </div>
      <!-- </router-link> -->
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      studentRouteList: [
        {
          name: "学习",
          routeName: "myStudy",
        },
        // {
        //   name: "完成学习",
        //   routeName: "myStudied",
        //   isDuring: false
        // },
      ],
      teacherRouteList: [
        {
          name: "教学",
          routeName: "myTeaching",
        },
        // {
        //   name: "完成教学",
        //   routeName: "myTaught",
        //   isDuring: false
        // },
      ],
      designRouteList: [
        {
          name: "设计",
          routeName: "myDesign",
        },
        // {
        //   name: "完成设计",
        //   routeName: "myDesigned",
        //   isDuring: false
        // },
      ],
      currPage: "",
    };
  },
  computed: {
    ...mapState({
      userInfo: (state) => state.user.userInfo,
      getPageName: (state) => state.page.myCoursePageName,
      chooseCourses: (state) => state.courses.chooseCourses,
      designcourselist: (state) => state.courses.designcourselist,
      myTeaching: (state) => state.courses.myTeaching,
      endTeaching: (state) => state.courses.endTeaching,
    }),
    filterRoute() {
      let routes = [];
      if (!this.userInfo.authNum) return;
      if (
        this.userInfo.authNum.indexOf(1) !== -1 ||
        this.userInfo.authNum.indexOf(256) !== -1 ||
        this.userInfo.authNum.indexOf(512) !== -1
      ) {
        routes = routes.concat(this.studentRouteList);
      }
      if (
        this.userInfo.authNum.indexOf(32) !== -1 ||
        this.userInfo.authNum.indexOf(2) !== -1 ||
        this.userInfo.authNum.indexOf(128) !== -1
      ) {
        routes = routes.concat(this.teacherRouteList);
      }
      if (this.userInfo.authNum.indexOf(64) !== -1) {
        routes = routes.concat(this.designRouteList);
      }
      return routes;
    },
    getCourseCount() {
      return function(name) {
        let count = {
          myStudy: this.chooseCourses.length,
          myTeaching: this.myTeaching.length + this.endTeaching.length,
          myDesign: this.designcourselist.length,
        }
        return count[name]
      }
    }
  },
  created() {
    this.ToCanGoRoute();
  },
  methods: {
    ToCanGoRoute(pageName) {
      if (this.getPageName) return this.currPage = this.getPageName
      if (this.$route.query.toPage === "myTeaching") {
        this.changePage("myTeaching");
        this.$router.push({ name: "MyCourses" });
      } else {
        if (this.$route.name == "MyCourses" && this.currPage == "") {
          if (this.userInfo.authNum?.indexOf(1) !== -1) {
            this.changePage("myStudy");
          } else {
            this.changePage("myTeaching");
          }
        }
      }
    },
    changePage(pageName) {
      this.currPage = pageName;
      this.$emit("handleChangePage", pageName);
    },
  },
};
</script>

<style lang="stylus">
.myCoursesPageNav {
  height: 80px;
  background-color: #ffffff;
  box-shadow: 0px 8px 12px 0px rgba(0, 0, 0, 0.16);

  .container {
    // padding: 0 0 10px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    max-width: 1200px;
    height: 100%;

    .flexBox {
      margin-right: 80px;
      display: flex;
      align-items: center;
      height: 27px;

      .title {
        cursor: pointer;
        margin-left: 5px;
        font-family: Microsoft YaHei;
        font-size: 16px;
        color: #003262;
      }

      .isActive {
        position: relative;
        font-weight: bold;
        font-family: Microsoft YaHei;
        font-size: 16px;
        color: #003262;
      }

      .isActive::after {
        position: absolute;
        content: '';
        left: 50%;
        right: 50%;
        bottom: -5px;
        width: 52px;
        height: 2px;
        background: #003262;
        transform: translate(-50%, 0);
      }
    }

    a:last-child {
      .flexBox {
        margin-right: 0;
      }
    }

    @media only screen and (max-width: 767px) {
      .flexBox {
        margin-right: 60px;
      }
    }

    .rightBox {
      margin-left: 205px;
    }
  }
}
</style>